package com.jxc.controller;

import com.jxc.VO.ResponceVO;
import com.jxc.pojo.Menu;
import com.jxc.pojo.Role;
import com.jxc.service.MenuService;
import com.jxc.service.RoleMenuService;
import com.jxc.service.RoleService;
import com.jxc.service.UserRoleService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private MenuService menuService;
    @RequestMapping("/list")
    public  List<Role> findRoles(String name){
        System.out.println("name==="+name);
        List<Role> roles = roleService.selectByExample(name);
        return roles;
    }

    @PostMapping
    public ResponceVO SaveRoles(Role role){
        ResponceVO responceVO = new ResponceVO();
        //添加角色
        if(role.getId() == null){
            //添加
            roleService.insert(role);
            responceVO.setSuccess(true);
            responceVO.setMessage("添加成功");
            return responceVO;
        }else if(role.getId() != null){
            //修改
            roleService.updateByPrimaryKey(role);
            responceVO.setSuccess(true);
            responceVO.setMessage("修改成功");
            return responceVO;
        }else{
            responceVO.setMessage("save:失败");
            return responceVO;
        }


    }

    @DeleteMapping
    public ResponceVO deleteRole(String id){
        ResponceVO responceVO = new ResponceVO();
        if(id != null){
            userRoleService.delUserRolesById(Integer.parseInt(id));//删除角色树中间表
            roleService.deleteByPrimaryKey(Integer.parseInt(id));//删除角色表
            responceVO.setSuccess(true);
            responceVO.setMessage("删除成功");
            return responceVO;
        }
        responceVO.setMessage("删除失败");
        return responceVO;
    }


    @RequestMapping("/loadCheckMenuInfo")
    public List<Menu> loadCheckMenuInfo(Integer parentId,Integer roleId){//角色唯一id
        List<Menu> menus = menuService.selectByExample(parentId,roleId);//菜单列表

        return  menus;
    }
}
